blob: bf5e79a54c3c295a83fed268e0564d8f299ae01a [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 Geissler131d04a2021-03-12 11:02:41 -06008#include <stdio.h>
Andrew Geisslere426b582020-05-28 12:40:55 -05009#include <systemd/sd-bus.h>
10
11#include <cereal/archives/json.hpp>
12#include <cereal/cereal.hpp>
13#include <cereal/types/string.hpp>
14#include <cereal/types/tuple.hpp>
15#include <cereal/types/vector.hpp>
16#include <phosphor-logging/elog-errors.hpp>
Andrew Geissler8ffdb262021-09-20 15:25:19 -050017#include <phosphor-logging/lg2.hpp>
Andrew Geisslere426b582020-05-28 12:40:55 -050018#include <sdbusplus/exception.hpp>
19#include <sdbusplus/server.hpp>
20#include <xyz/openbmc_project/Common/error.hpp>
21#include <xyz/openbmc_project/Control/Power/RestorePolicy/server.hpp>
Andrew Geissler765446a2023-05-25 15:38:20 -040022#include <xyz/openbmc_project/State/Host/error.hpp>
Andrew Geisslere426b582020-05-28 12:40:55 -050023
Andrew Geissler131d04a2021-03-12 11:02:41 -060024#include <filesystem>
Patrick Williams78c066f2024-02-13 12:25:58 -060025#include <format>
Andrew Geisslere426b582020-05-28 12:40:55 -050026#include <fstream>
Andrew Geissler36529022016-11-29 15:23:54 -060027#include <iostream>
Andrew Geissler0cd2eaf2016-12-07 10:50:13 -060028#include <map>
Andrew Geissler54ce6392024-02-23 10:09:09 -060029#include <set>
Andrew Geissler0cd2eaf2016-12-07 10:50:13 -060030#include <string>
Dhruvaraj Subhashchandran3f475242017-07-12 00:44:27 -050031
Vishwanatha Subbanna0a838732017-10-05 12:43:19 +053032// Register class version with Cereal
Andrew Geissler769a62f2019-12-06 13:36:08 -060033CEREAL_CLASS_VERSION(phosphor::state::manager::Host, CLASS_VERSION)
Andrew Geissler36529022016-11-29 15:23:54 -060034
35namespace phosphor
36{
37namespace state
38{
39namespace manager
40{
41
Andrew Geissler8ffdb262021-09-20 15:25:19 -050042PHOSPHOR_LOG2_USING;
43
Andrew Geissler7b90a622017-08-08 11:41:08 -050044// When you see server:: or reboot:: you know we're referencing our base class
Patrick Williams7e969cb2023-08-23 16:24:23 -050045namespace server = sdbusplus::server::xyz::openbmc_project::state;
46namespace reboot = sdbusplus::server::xyz::openbmc_project::control::boot;
47namespace bootprogress = sdbusplus::server::xyz::openbmc_project::state::boot;
Dhruvaraj Subhashchandrana3b8d7e2017-08-10 05:40:04 -050048namespace osstatus =
Patrick Williams7e969cb2023-08-23 16:24:23 -050049 sdbusplus::server::xyz::openbmc_project::state::operating_system;
Andrew Geissler1e3bf942016-12-13 15:32:22 -060050using namespace phosphor::logging;
Patrick Williams6ed41ea2022-04-05 15:50:21 -050051namespace fs = std::filesystem;
Andrew Geissler2f60aae2019-09-12 13:25:21 -050052using sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
Andrew Geissler1e3bf942016-12-13 15:32:22 -060053
Josh D. King929ef702017-03-02 10:58:11 -060054constexpr auto ACTIVE_STATE = "active";
55constexpr auto ACTIVATING_STATE = "activating";
56
Andrew Geissler58a18012018-01-19 19:36:05 -080057constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1";
58constexpr auto SYSTEMD_OBJ_PATH = "/org/freedesktop/systemd1";
Andrew Geissler0cd2eaf2016-12-07 10:50:13 -060059constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager";
60
Josh D. King929ef702017-03-02 10:58:11 -060061constexpr auto SYSTEMD_PROPERTY_IFACE = "org.freedesktop.DBus.Properties";
62constexpr auto SYSTEMD_INTERFACE_UNIT = "org.freedesktop.systemd1.Unit";
63
Andrew Geissleref3c1842016-12-01 12:33:09 -060064void Host::determineInitialState()
65{
Allen.Wang79b45002022-02-10 17:59:20 +080066 if (stateActive(getTarget(server::Host::HostState::Running)) ||
Potin Lai70f36d82022-03-15 10:25:39 +080067 isHostRunning(id))
Andrew Geissleref3c1842016-12-01 12:33:09 -060068 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -050069 info("Initial Host State will be Running");
Andrew Geissler97924142017-01-24 16:10:18 -060070 server::Host::currentHostState(HostState::Running);
71 server::Host::requestedHostTransition(Transition::On);
Andrew Geissleref3c1842016-12-01 12:33:09 -060072 }
73 else
74 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -050075 info("Initial Host State will be Off");
Andrew Geissler97924142017-01-24 16:10:18 -060076 server::Host::currentHostState(HostState::Off);
77 server::Host::requestedHostTransition(Transition::Off);
Andrew Geissleref3c1842016-12-01 12:33:09 -060078 }
79
Allen.Wangba182f02022-03-23 19:01:53 +080080 if (!deserialize())
Dhruvaraj Subhashchandran3f475242017-07-12 00:44:27 -050081 {
Andrew Geissler58a18012018-01-19 19:36:05 -080082 // set to default value.
Dhruvaraj Subhashchandran3f475242017-07-12 00:44:27 -050083 server::Host::requestedHostTransition(Transition::Off);
84 }
Andrew Geissleref3c1842016-12-01 12:33:09 -060085 return;
86}
87
Andrew Geissler54ce6392024-02-23 10:09:09 -060088void Host::setupSupportedTransitions()
89{
90 std::set<Transition> supportedTransitions = {
91 Transition::On, Transition::Off, Transition::Reboot,
92 Transition::GracefulWarmReboot, Transition::ForceWarmReboot};
93 server::Host::allowedHostTransitions(supportedTransitions);
94}
95
Allen.Wang79b45002022-02-10 17:59:20 +080096void Host::createSystemdTargetMaps()
97{
98 stateTargetTable = {
Patrick Williams78c066f2024-02-13 12:25:58 -060099 {HostState::Off, std::format("obmc-host-stop@{}.target", id)},
100 {HostState::Running, std::format("obmc-host-startmin@{}.target", id)},
101 {HostState::Quiesced, std::format("obmc-host-quiesce@{}.target", id)},
Allen.Wang79b45002022-02-10 17:59:20 +0800102 {HostState::DiagnosticMode,
Patrick Williams78c066f2024-02-13 12:25:58 -0600103 std::format("obmc-host-diagnostic-mode@{}.target", id)}};
Allen.Wang79b45002022-02-10 17:59:20 +0800104
105 transitionTargetTable = {
Patrick Williams78c066f2024-02-13 12:25:58 -0600106 {Transition::Off, std::format("obmc-host-shutdown@{}.target", id)},
107 {Transition::On, std::format("obmc-host-start@{}.target", id)},
108 {Transition::Reboot, std::format("obmc-host-reboot@{}.target", id)},
Allen.Wang79b45002022-02-10 17:59:20 +0800109// Some systems do not support a warm reboot so just map the reboot
110// requests to our normal cold reboot in that case
111#if ENABLE_WARM_REBOOT
112 {Transition::GracefulWarmReboot,
Patrick Williams78c066f2024-02-13 12:25:58 -0600113 std::format("obmc-host-warm-reboot@{}.target", id)},
Allen.Wang79b45002022-02-10 17:59:20 +0800114 {Transition::ForceWarmReboot,
Patrick Williams78c066f2024-02-13 12:25:58 -0600115 std::format("obmc-host-force-warm-reboot@{}.target", id)}
Allen.Wang79b45002022-02-10 17:59:20 +0800116 };
117#else
118 {Transition::GracefulWarmReboot,
Patrick Williams78c066f2024-02-13 12:25:58 -0600119 std::format("obmc-host-reboot@{}.target", id)},
Allen.Wang79b45002022-02-10 17:59:20 +0800120 {Transition::ForceWarmReboot,
Patrick Williams78c066f2024-02-13 12:25:58 -0600121 std::format("obmc-host-reboot@{}.target", id)}
Allen.Wang79b45002022-02-10 17:59:20 +0800122 };
123#endif
Patrick Williams78c066f2024-02-13 12:25:58 -0600124 hostCrashTarget = std::format("obmc-host-crash@{}.target", id);
Allen.Wang79b45002022-02-10 17:59:20 +0800125}
126
127const std::string& Host::getTarget(HostState state)
128{
129 return stateTargetTable[state];
130};
131
132const std::string& Host::getTarget(Transition tranReq)
133{
134 return transitionTargetTable[tranReq];
135};
136
Andrew Geissler0cd2eaf2016-12-07 10:50:13 -0600137void Host::executeTransition(Transition tranReq)
138{
Allen.Wang79b45002022-02-10 17:59:20 +0800139 auto& sysdUnit = getTarget(tranReq);
Andrew Geissler0cd2eaf2016-12-07 10:50:13 -0600140
Andrew Geissler58a18012018-01-19 19:36:05 -0800141 auto method = this->bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH,
142 SYSTEMD_INTERFACE, "StartUnit");
Andrew Geissler0cd2eaf2016-12-07 10:50:13 -0600143
144 method.append(sysdUnit);
145 method.append("replace");
146
Andrew Geissler4da7e002017-01-24 15:21:40 -0600147 this->bus.call_noreply(method);
Andrew Geissler0cd2eaf2016-12-07 10:50:13 -0600148
149 return;
150}
151
Josh D. King929ef702017-03-02 10:58:11 -0600152bool Host::stateActive(const std::string& target)
153{
Patrick Williams2975e262020-05-13 18:01:09 -0500154 std::variant<std::string> currentState;
Josh D. King929ef702017-03-02 10:58:11 -0600155 sdbusplus::message::object_path unitTargetPath;
156
Andrew Geissler58a18012018-01-19 19:36:05 -0800157 auto method = this->bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH,
158 SYSTEMD_INTERFACE, "GetUnit");
Josh D. King929ef702017-03-02 10:58:11 -0600159
160 method.append(target);
Josh D. King929ef702017-03-02 10:58:11 -0600161
Anthony Wilson32c532e2018-10-25 21:56:07 -0500162 try
Josh D. King929ef702017-03-02 10:58:11 -0600163 {
Anthony Wilson32c532e2018-10-25 21:56:07 -0500164 auto result = this->bus.call(method);
165 result.read(unitTargetPath);
166 }
Patrick Williamsf053e6f2022-07-22 19:26:54 -0500167 catch (const sdbusplus::exception_t& e)
Anthony Wilson32c532e2018-10-25 21:56:07 -0500168 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500169 error("Error in GetUnit call: {ERROR}", "ERROR", e);
Josh D. King929ef702017-03-02 10:58:11 -0600170 return false;
171 }
172
Andrew Geissler58a18012018-01-19 19:36:05 -0800173 method = this->bus.new_method_call(
174 SYSTEMD_SERVICE,
175 static_cast<const std::string&>(unitTargetPath).c_str(),
176 SYSTEMD_PROPERTY_IFACE, "Get");
Josh D. King929ef702017-03-02 10:58:11 -0600177
178 method.append(SYSTEMD_INTERFACE_UNIT, "ActiveState");
Josh D. King929ef702017-03-02 10:58:11 -0600179
Anthony Wilson32c532e2018-10-25 21:56:07 -0500180 try
Josh D. King929ef702017-03-02 10:58:11 -0600181 {
Anthony Wilson32c532e2018-10-25 21:56:07 -0500182 auto result = this->bus.call(method);
183 result.read(currentState);
184 }
Patrick Williamsf053e6f2022-07-22 19:26:54 -0500185 catch (const sdbusplus::exception_t& e)
Anthony Wilson32c532e2018-10-25 21:56:07 -0500186 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500187 error("Error in ActiveState Get: {ERROR}", "ERROR", e);
Josh D. King929ef702017-03-02 10:58:11 -0600188 return false;
189 }
190
Patrick Williams37413dc2020-05-13 11:29:54 -0500191 const auto& currentStateStr = std::get<std::string>(currentState);
William A. Kennington III7a0689a2018-11-12 17:19:33 -0800192 return currentStateStr == ACTIVE_STATE ||
193 currentStateStr == ACTIVATING_STATE;
Josh D. King929ef702017-03-02 10:58:11 -0600194}
195
Michael Tritz206a8332017-02-06 16:01:23 -0600196bool Host::isAutoReboot()
197{
Deepak Kodihalli3dd08a52017-07-25 07:34:44 -0500198 using namespace settings;
Michael Tritz206a8332017-02-06 16:01:23 -0600199
Andrew Geissler7f620832020-10-23 08:25:52 -0500200 /* The logic here is to first check the one-time AutoReboot setting.
201 * If this property is true (the default) then look at the persistent
202 * user setting in the non one-time object, otherwise honor the one-time
203 * setting and do not auto reboot.
204 */
205 auto methodOneTime = bus.new_method_call(
Andrew Geissler58a18012018-01-19 19:36:05 -0800206 settings.service(settings.autoReboot, autoRebootIntf).c_str(),
Andrew Geissler7f620832020-10-23 08:25:52 -0500207 settings.autoRebootOneTime.c_str(), SYSTEMD_PROPERTY_IFACE, "Get");
208 methodOneTime.append(autoRebootIntf, "AutoReboot");
209
210 auto methodUserSetting = bus.new_method_call(
211 settings.service(settings.autoReboot, autoRebootIntf).c_str(),
212 settings.autoReboot.c_str(), SYSTEMD_PROPERTY_IFACE, "Get");
213 methodUserSetting.append(autoRebootIntf, "AutoReboot");
Michael Tritz206a8332017-02-06 16:01:23 -0600214
Anthony Wilson32c532e2018-10-25 21:56:07 -0500215 try
Michael Tritz206a8332017-02-06 16:01:23 -0600216 {
Andrew Geissler7f620832020-10-23 08:25:52 -0500217 auto reply = bus.call(methodOneTime);
Patrick Williams2975e262020-05-13 18:01:09 -0500218 std::variant<bool> result;
Anthony Wilson32c532e2018-10-25 21:56:07 -0500219 reply.read(result);
Patrick Williams37413dc2020-05-13 11:29:54 -0500220 auto autoReboot = std::get<bool>(result);
Andrew Geissler7f620832020-10-23 08:25:52 -0500221
222 if (!autoReboot)
223 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500224 info("Auto reboot (one-time) disabled");
Andrew Geissler7f620832020-10-23 08:25:52 -0500225 return false;
226 }
227 else
228 {
229 // one-time is true so read the user setting
230 reply = bus.call(methodUserSetting);
231 reply.read(result);
232 autoReboot = std::get<bool>(result);
233 }
234
Anthony Wilson32c532e2018-10-25 21:56:07 -0500235 auto rebootCounterParam = reboot::RebootAttempts::attemptsLeft();
236
237 if (autoReboot)
Saqib Khancbe08d12017-03-10 01:29:20 -0600238 {
Anthony Wilson32c532e2018-10-25 21:56:07 -0500239 if (rebootCounterParam > 0)
240 {
241 // Reduce BOOTCOUNT by 1
Andrew Geisslerad65b2d2021-09-21 12:53:29 -0500242 info(
243 "Auto reboot enabled and boot count at {BOOTCOUNT}, rebooting",
244 "BOOTCOUNT", rebootCounterParam);
Anthony Wilson32c532e2018-10-25 21:56:07 -0500245 return true;
246 }
Anthony Wilson32c532e2018-10-25 21:56:07 -0500247 else
248 {
Andrew Geissler43284792021-09-23 10:32:15 -0500249 // We are at 0 so reset reboot counter and go to quiesce state
250 info("Auto reboot enabled but HOST BOOTCOUNT already set to 0");
NodeMan97b4cbfac2022-05-09 23:56:39 -0500251 attemptsLeft(reboot::RebootAttempts::retryAttempts());
Andrew Geissler1ab2b6c2022-03-10 16:16:15 -0600252
253 // Generate log since we will now be sitting in Quiesce
254 const std::string errorMsg =
255 "xyz.openbmc_project.State.Error.HostQuiesce";
256 utils::createError(this->bus, errorMsg,
257 sdbusplus::xyz::openbmc_project::Logging::
258 server::Entry::Level::Critical);
Andrew Geissler5bcaee12022-04-19 15:40:40 -0400259
260 // Generate BMC dump to assist with debug
261 utils::createBmcDump(this->bus);
262
Anthony Wilson32c532e2018-10-25 21:56:07 -0500263 return false;
264 }
Saqib Khand5ac6352017-04-04 09:53:59 -0500265 }
266 else
267 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500268 info("Auto reboot disabled.");
Saqib Khancbe08d12017-03-10 01:29:20 -0600269 return false;
270 }
Michael Tritz206a8332017-02-06 16:01:23 -0600271 }
Patrick Williamsf053e6f2022-07-22 19:26:54 -0500272 catch (const sdbusplus::exception_t& e)
Saqib Khand5ac6352017-04-04 09:53:59 -0500273 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500274 error("Error in AutoReboot Get, {ERROR}", "ERROR", e);
Saqib Khand5ac6352017-04-04 09:53:59 -0500275 return false;
276 }
Michael Tritz206a8332017-02-06 16:01:23 -0600277}
278
Patrick Williamsf053e6f2022-07-22 19:26:54 -0500279void Host::sysStateChangeJobRemoved(sdbusplus::message_t& msg)
Andrew Geissler4da7e002017-01-24 15:21:40 -0600280{
Andrew Geissler58a18012018-01-19 19:36:05 -0800281 uint32_t newStateID{};
Andrew Geissler4da7e002017-01-24 15:21:40 -0600282 sdbusplus::message::object_path newStateObjPath;
283 std::string newStateUnit{};
284 std::string newStateResult{};
285
Andrew Geissler58a18012018-01-19 19:36:05 -0800286 // Read the msg and populate each variable
Patrick Williamsd22706f2017-05-04 05:42:49 -0500287 msg.read(newStateID, newStateObjPath, newStateUnit, newStateResult);
Andrew Geissler4da7e002017-01-24 15:21:40 -0600288
Allen.Wang79b45002022-02-10 17:59:20 +0800289 if ((newStateUnit == getTarget(server::Host::HostState::Off)) &&
Andrew Geissler969b2612018-03-29 10:16:51 -0700290 (newStateResult == "done") &&
Allen.Wang79b45002022-02-10 17:59:20 +0800291 (!stateActive(getTarget(server::Host::HostState::Running))))
Andrew Geissleref621162016-12-08 12:56:21 -0600292 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500293 info("Received signal that host is off");
Andrew Geissler4da7e002017-01-24 15:21:40 -0600294 this->currentHostState(server::Host::HostState::Off);
Dhruvaraj Subhashchandrana3b8d7e2017-08-10 05:40:04 -0500295 this->bootProgress(bootprogress::Progress::ProgressStages::Unspecified);
296 this->operatingSystemState(osstatus::Status::OSStatus::Inactive);
Andrew Geissleref621162016-12-08 12:56:21 -0600297 }
Allen.Wang79b45002022-02-10 17:59:20 +0800298 else if ((newStateUnit == getTarget(server::Host::HostState::Running)) &&
Andrew Geissler58a18012018-01-19 19:36:05 -0800299 (newStateResult == "done") &&
Allen.Wang79b45002022-02-10 17:59:20 +0800300 (stateActive(getTarget(server::Host::HostState::Running))))
Andrew Geissler58a18012018-01-19 19:36:05 -0800301 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500302 info("Received signal that host is running");
Andrew Geissler58a18012018-01-19 19:36:05 -0800303 this->currentHostState(server::Host::HostState::Running);
Andrew Geissler131d04a2021-03-12 11:02:41 -0600304
305 // Remove temporary file which is utilized for scenarios where the
306 // BMC is rebooted while the host is still up.
307 // This file is used to indicate to host related systemd services
308 // that the host is already running and they should skip running.
309 // Once the host state is back to running we can clear this file.
Patrick Williams78c066f2024-02-13 12:25:58 -0600310 std::string hostFile = std::format(HOST_RUNNING_FILE, 0);
311 if (std::filesystem::exists(hostFile))
Andrew Geissler131d04a2021-03-12 11:02:41 -0600312 {
Patrick Williams78c066f2024-02-13 12:25:58 -0600313 std::filesystem::remove(hostFile);
Andrew Geissler131d04a2021-03-12 11:02:41 -0600314 }
Andrew Geissler58a18012018-01-19 19:36:05 -0800315 }
Allen.Wang79b45002022-02-10 17:59:20 +0800316 else if ((newStateUnit == getTarget(server::Host::HostState::Quiesced)) &&
Josh D. King29d025d2017-04-27 12:40:22 -0500317 (newStateResult == "done") &&
Allen.Wang79b45002022-02-10 17:59:20 +0800318 (stateActive(getTarget(server::Host::HostState::Quiesced))))
Andrew Geissler58a18012018-01-19 19:36:05 -0800319 {
320 if (Host::isAutoReboot())
321 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500322 info("Beginning reboot...");
Andrew Geissler58a18012018-01-19 19:36:05 -0800323 Host::requestedHostTransition(server::Host::Transition::Reboot);
324 }
325 else
326 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500327 info("Maintaining quiesce");
Andrew Geissler58a18012018-01-19 19:36:05 -0800328 this->currentHostState(server::Host::HostState::Quiesced);
329 }
330 }
Andrew Geissleref621162016-12-08 12:56:21 -0600331}
332
Patrick Williamsf053e6f2022-07-22 19:26:54 -0500333void Host::sysStateChangeJobNew(sdbusplus::message_t& msg)
Andrew Geissler47b96122020-02-11 16:13:13 -0600334{
335 uint32_t newStateID{};
336 sdbusplus::message::object_path newStateObjPath;
337 std::string newStateUnit{};
338
339 // Read the msg and populate each variable
340 msg.read(newStateID, newStateObjPath, newStateUnit);
341
Allen.Wang79b45002022-02-10 17:59:20 +0800342 if (newStateUnit == getTarget(server::Host::HostState::DiagnosticMode))
Andrew Geissler47b96122020-02-11 16:13:13 -0600343 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500344 info("Received signal that host is in diagnostice mode");
Andrew Geissler47b96122020-02-11 16:13:13 -0600345 this->currentHostState(server::Host::HostState::DiagnosticMode);
346 }
Andrew Geissler128ea8e2022-06-22 12:28:15 -0400347 else if ((newStateUnit == hostCrashTarget) &&
348 (server::Host::currentHostState() ==
349 server::Host::HostState::Running))
350 {
351 // Only decrease the boot count if host was running when the host crash
352 // target was started. Systemd will sometimes trigger multiple
353 // JobNew events for the same target. This seems to be related to
354 // how OpenBMC utilizes the targets in the reboot scenario
355 info("Received signal that host has crashed, decrement reboot count");
356
357 // A host crash can cause a reboot of the host so decrement the reboot
358 // count
359 decrementRebootCount();
360 }
Andrew Geissler47b96122020-02-11 16:13:13 -0600361}
362
Andrew Geissler7b90a622017-08-08 11:41:08 -0500363uint32_t Host::decrementRebootCount()
364{
365 auto rebootCount = reboot::RebootAttempts::attemptsLeft();
Andrew Geissler58a18012018-01-19 19:36:05 -0800366 if (rebootCount > 0)
Andrew Geissler7b90a622017-08-08 11:41:08 -0500367 {
Andrew Geissler58a18012018-01-19 19:36:05 -0800368 return (reboot::RebootAttempts::attemptsLeft(rebootCount - 1));
Andrew Geissler7b90a622017-08-08 11:41:08 -0500369 }
370 return rebootCount;
371}
372
Allen.Wangba182f02022-03-23 19:01:53 +0800373fs::path Host::serialize()
Andrew Geissler033fc3b2017-08-30 15:11:44 -0500374{
Patrick Williams78c066f2024-02-13 12:25:58 -0600375 fs::path path{std::format(HOST_STATE_PERSIST_PATH, id)};
Allen.Wangba182f02022-03-23 19:01:53 +0800376 std::ofstream os(path.c_str(), std::ios::binary);
Andrew Geissler033fc3b2017-08-30 15:11:44 -0500377 cereal::JSONOutputArchive oarchive(os);
378 oarchive(*this);
Allen.Wangba182f02022-03-23 19:01:53 +0800379 return path;
Andrew Geissler033fc3b2017-08-30 15:11:44 -0500380}
381
Allen.Wangba182f02022-03-23 19:01:53 +0800382bool Host::deserialize()
Andrew Geissler033fc3b2017-08-30 15:11:44 -0500383{
Patrick Williams78c066f2024-02-13 12:25:58 -0600384 fs::path path{std::format(HOST_STATE_PERSIST_PATH, id)};
Jayanth Othayothe39f3792017-09-19 23:53:31 -0500385 try
Andrew Geissler033fc3b2017-08-30 15:11:44 -0500386 {
Jayanth Othayothe39f3792017-09-19 23:53:31 -0500387 if (fs::exists(path))
388 {
389 std::ifstream is(path.c_str(), std::ios::in | std::ios::binary);
390 cereal::JSONInputArchive iarchive(is);
391 iarchive(*this);
392 return true;
393 }
394 return false;
Andrew Geissler033fc3b2017-08-30 15:11:44 -0500395 }
Patrick Williams8583b3b2021-10-06 12:19:20 -0500396 catch (const cereal::Exception& e)
Jayanth Othayothe39f3792017-09-19 23:53:31 -0500397 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500398 error("deserialize exception: {ERROR}", "ERROR", e);
Jayanth Othayothe39f3792017-09-19 23:53:31 -0500399 fs::remove(path);
400 return false;
401 }
Andrew Geissler033fc3b2017-08-30 15:11:44 -0500402}
403
Andrew Geissleref3c1842016-12-01 12:33:09 -0600404Host::Transition Host::requestedHostTransition(Transition value)
405{
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500406 info("Host state transition request of {REQ}", "REQ", value);
Andrew Geissler765446a2023-05-25 15:38:20 -0400407
408#if ONLY_ALLOW_BOOT_WHEN_BMC_READY
409 if ((value != Transition::Off) && (!utils::isBmcReady(this->bus)))
410 {
411 info("BMC State is not Ready so no host on operations allowed");
412 throw sdbusplus::xyz::openbmc_project::State::Host::Error::
413 BMCNotReady();
414 }
415#endif
416
Andrew Geissler7b90a622017-08-08 11:41:08 -0500417 // If this is not a power off request then we need to
418 // decrement the reboot counter. This code should
419 // never prevent a power on, it should just decrement
420 // the count to 0. The quiesce handling is where the
421 // check of this count will occur
Andrew Geissler58a18012018-01-19 19:36:05 -0800422 if (value != server::Host::Transition::Off)
Andrew Geissler7b90a622017-08-08 11:41:08 -0500423 {
424 decrementRebootCount();
425 }
426
Andrew Geisslera27a6e82017-07-27 16:44:43 -0500427 executeTransition(value);
Andrew Geissler7b90a622017-08-08 11:41:08 -0500428
Andrew Geissler58a18012018-01-19 19:36:05 -0800429 auto retVal = server::Host::requestedHostTransition(value);
Allen.Wangba182f02022-03-23 19:01:53 +0800430
Andrew Geissler033fc3b2017-08-30 15:11:44 -0500431 serialize();
Dhruvaraj Subhashchandran3f475242017-07-12 00:44:27 -0500432 return retVal;
Andrew Geissleref3c1842016-12-01 12:33:09 -0600433}
434
Dhruvaraj Subhashchandran4e6534f2017-09-19 06:13:20 -0500435Host::ProgressStages Host::bootProgress(ProgressStages value)
436{
437 auto retVal = bootprogress::Progress::bootProgress(value);
438 serialize();
439 return retVal;
440}
441
442Host::OSStatus Host::operatingSystemState(OSStatus value)
443{
444 auto retVal = osstatus::Status::operatingSystemState(value);
445 serialize();
446 return retVal;
447}
448
Andrew Geissleref3c1842016-12-01 12:33:09 -0600449Host::HostState Host::currentHostState(HostState value)
450{
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500451 info("Change to Host State: {STATE}", "STATE", value);
Andrew Geissleref621162016-12-08 12:56:21 -0600452 return server::Host::currentHostState(value);
Andrew Geissleref3c1842016-12-01 12:33:09 -0600453}
454
Andrew Geissler36529022016-11-29 15:23:54 -0600455} // namespace manager
456} // namespace state
Andrew Geisslera965cf02018-08-31 08:37:05 -0700457} // namespace phosphor