1 #include "config.h"
2
3 #include "host_state_manager.hpp"
4 #include "settings.hpp"
5 #include "utils.hpp"
6 #include "xyz/openbmc_project/Common/error.hpp"
7 #include "xyz/openbmc_project/Control/Power/RestorePolicy/server.hpp"
8
9 #include <getopt.h>
10 #include <systemd/sd-bus.h>
11
12 #include <phosphor-logging/elog-errors.hpp>
13 #include <phosphor-logging/lg2.hpp>
14 #include <sdbusplus/exception.hpp>
15 #include <sdbusplus/server.hpp>
16 #include <xyz/openbmc_project/State/BMC/client.hpp>
17 #include <xyz/openbmc_project/State/Host/client.hpp>
18
19 #include <filesystem>
20 #include <iostream>
21 #include <map>
22 #include <string>
23 #include <thread>
24
25 namespace phosphor
26 {
27 namespace state
28 {
29 namespace manager
30 {
31
32 PHOSPHOR_LOG2_USING;
33
34 using namespace phosphor::logging;
35 using namespace sdbusplus::xyz::openbmc_project::Common::Error;
36 using namespace sdbusplus::server::xyz::openbmc_project::control::power;
37 using HostState = sdbusplus::client::xyz::openbmc_project::state::Host<>;
38 using BMCState = sdbusplus::client::xyz::openbmc_project::state::BMC<>;
39
40 } // namespace manager
41 } // namespace state
42 } // namespace phosphor
43
main(int argc,char ** argv)44 int main(int argc, char** argv)
45 {
46 using namespace phosphor::logging;
47
48 size_t hostId = 0;
49 int arg;
50 int optIndex = 0;
51
52 static struct option longOpts[] = {
53 {"host", required_argument, nullptr, 'h'}, {nullptr, 0, nullptr, 0}};
54
55 while ((arg = getopt_long(argc, argv, "h:", longOpts, &optIndex)) != -1)
56 {
57 switch (arg)
58 {
59 case 'h':
60 hostId = std::stoul(optarg);
61 break;
62 default:
63 break;
64 }
65 }
66
67 using Host = sdbusplus::client::xyz::openbmc_project::state::Host<>;
68 std::string hostPath =
69 std::string(Host::namespace_path::value) + "/" +
70 std::string(Host::namespace_path::host) + std::to_string(hostId);
71
72 auto bus = sdbusplus::bus::new_default();
73
74 using namespace settings;
75 HostObjects settings(bus, hostId);
76
77 using namespace phosphor::state::manager;
78 namespace server = sdbusplus::server::xyz::openbmc_project::state;
79
80 // This application is only run if chassis power is off
81
82 // If the BMC was rebooted due to a user initiated pinhole reset, do not
83 // implement any power restore policies
84 using BMC = sdbusplus::client::xyz::openbmc_project::state::BMC<>;
85 auto bmcPath = sdbusplus::message::object_path(BMC::namespace_path::value) /
86 BMC::namespace_path::bmc;
87
88 #if !(RUN_APR_ON_PINHOLE_RESET && RUN_APR_ON_WATCHDOG_RESET && \
89 RUN_APR_ON_SOFTWARE_RESET)
90 auto bmcRebootCause =
91 sdbusplus::message::convert_from_string<BMC::RebootCause>(
92 phosphor::state::manager::utils::getProperty(
93 bus, bmcPath.str, BMCState::interface, "LastRebootCause"));
94
95 #if !RUN_APR_ON_PINHOLE_RESET
96 if (bmcRebootCause == BMC::RebootCause::PinholeReset)
97 {
98 info(
99 "BMC was reset due to pinhole reset, no power restore policy will be run");
100 return 0;
101 }
102 #endif // RUN_APR_ON_PINHOLE_RESET
103
104 #if !RUN_APR_ON_WATCHDOG_RESET
105 if (bmcRebootCause == BMC::RebootCause::Watchdog)
106 {
107 info(
108 "BMC was reset due to watchdog, no power restore policy will be run");
109 return 0;
110 }
111 #endif // RUN_APR_ON_WATCHDOG_RESET
112
113 #if !RUN_APR_ON_SOFTWARE_RESET
114 if (bmcRebootCause == BMC::RebootCause::Software)
115 {
116 info(
117 "BMC was reset due to cold reset, no power restore policy will be run");
118 return 0;
119 }
120 #endif // RUN_APR_ON_SOFTWARE_RESET
121 #endif
122
123 /* The logic here is to first check the one-time PowerRestorePolicy setting.
124 * If this property is not the default then look at the persistent
125 * user setting in the non one-time object, otherwise honor the one-time
126 * setting.
127 */
128 auto methodOneTime = bus.new_method_call(
129 settings.service(settings.powerRestorePolicy, powerRestoreIntf).c_str(),
130 settings.powerRestorePolicyOneTime.c_str(),
131 "org.freedesktop.DBus.Properties", "Get");
132 methodOneTime.append(powerRestoreIntf, "PowerRestorePolicy");
133
134 auto methodUserSetting = bus.new_method_call(
135 settings.service(settings.powerRestorePolicy, powerRestoreIntf).c_str(),
136 settings.powerRestorePolicy.c_str(), "org.freedesktop.DBus.Properties",
137 "Get");
138 methodUserSetting.append(powerRestoreIntf, "PowerRestorePolicy");
139
140 std::variant<std::string> result;
141 try
142 {
143 auto reply = bus.call(methodOneTime);
144 reply.read(result);
145 auto powerPolicy = std::get<std::string>(result);
146
147 if (RestorePolicy::Policy::None ==
148 RestorePolicy::convertPolicyFromString(powerPolicy))
149 {
150 // one_time is set to None so use the customer setting
151 info("One time not set, check user setting of power policy");
152
153 auto reply = bus.call(methodUserSetting);
154 reply.read(result);
155 powerPolicy = std::get<std::string>(result);
156 }
157 else
158 {
159 // one_time setting was set so we're going to use it. Reset it
160 // to default for next time.
161 info("One time set, use it and reset to default");
162 phosphor::state::manager::utils::setProperty(
163 bus, settings.powerRestorePolicyOneTime, powerRestoreIntf,
164 "PowerRestorePolicy",
165 convertForMessage(RestorePolicy::Policy::None));
166 }
167
168 auto methodUserSettingDelay = bus.new_method_call(
169 settings.service(settings.powerRestorePolicy, powerRestoreIntf)
170 .c_str(),
171 settings.powerRestorePolicy.c_str(),
172 "org.freedesktop.DBus.Properties", "Get");
173
174 methodUserSettingDelay.append(powerRestoreIntf, "PowerRestoreDelay");
175
176 std::variant<uint64_t> restoreDelay;
177
178 auto delayResult = bus.call(methodUserSettingDelay);
179 delayResult.read(restoreDelay);
180 auto powerRestoreDelayUsec =
181 std::chrono::microseconds(std::get<uint64_t>(restoreDelay));
182 auto powerRestoreDelaySec =
183 std::chrono::duration_cast<std::chrono::seconds>(
184 powerRestoreDelayUsec);
185
186 info("Host power is off, processing power policy {POWER_POLICY}",
187 "POWER_POLICY", powerPolicy);
188
189 if (RestorePolicy::Policy::AlwaysOn ==
190 RestorePolicy::convertPolicyFromString(powerPolicy))
191 {
192 info(
193 "power_policy=ALWAYS_POWER_ON, powering host on ({DELAY}s delay)",
194 "DELAY", powerRestoreDelaySec.count());
195 #ifdef APPLY_POWER_POLICY_WHEN_BMC_READY
196 utils::waitBmcReady(bus, powerRestoreDelaySec);
197 #else
198 std::this_thread::sleep_for(powerRestoreDelayUsec);
199 #endif
200 phosphor::state::manager::utils::setProperty(
201 bus, hostPath, HostState::interface, "RestartCause",
202 convertForMessage(
203 server::Host::RestartCause::PowerPolicyAlwaysOn));
204 phosphor::state::manager::utils::setProperty(
205 bus, hostPath, HostState::interface, "RequestedHostTransition",
206 convertForMessage(server::Host::Transition::On));
207 }
208 // Always execute power on if AlwaysOn is set, otherwise check config
209 // option (and AC loss status) on whether to execute other policy
210 // settings
211 #if ONLY_RUN_APR_ON_POWER_LOSS
212 else if (!phosphor::state::manager::utils::checkACLoss(hostId))
213 {
214 info(
215 "Chassis power was not on prior to BMC reboot so do not run any further power policy");
216 return 0;
217 }
218 #endif
219 else if (RestorePolicy::Policy::AlwaysOff ==
220 RestorePolicy::convertPolicyFromString(powerPolicy))
221 {
222 info(
223 "power_policy=ALWAYS_POWER_OFF, set requested state to off ({DELAY}s delay)",
224 "DELAY", powerRestoreDelaySec.count());
225 #ifdef APPLY_POWER_POLICY_WHEN_BMC_READY
226 utils::waitBmcReady(bus, powerRestoreDelaySec);
227 #else
228 std::this_thread::sleep_for(powerRestoreDelayUsec);
229 #endif
230 // Read last requested state and re-request it to execute it
231 auto hostReqState = phosphor::state::manager::utils::getProperty(
232 bus, hostPath, HostState::interface, "RequestedHostTransition");
233 if (hostReqState !=
234 convertForMessage(server::Host::Transition::Off))
235 {
236 phosphor::state::manager::utils::setProperty(
237 bus, hostPath, HostState::interface,
238 "RequestedHostTransition",
239 convertForMessage(server::Host::Transition::Off));
240 }
241 }
242 else if (RestorePolicy::Policy::Restore ==
243 RestorePolicy::convertPolicyFromString(powerPolicy))
244 {
245 info("power_policy=RESTORE, restoring last state ({DELAY}s delay)",
246 "DELAY", powerRestoreDelaySec.count());
247 #ifdef APPLY_POWER_POLICY_WHEN_BMC_READY
248 utils::waitBmcReady(bus, powerRestoreDelaySec);
249 #else
250 std::this_thread::sleep_for(powerRestoreDelayUsec);
251 #endif
252 // Read last requested state and re-request it to execute it
253 auto hostReqState = phosphor::state::manager::utils::getProperty(
254 bus, hostPath, HostState::interface, "RequestedHostTransition");
255
256 // As long as the host transition is not 'Off' power on host state.
257 if (hostReqState !=
258 convertForMessage(server::Host::Transition::Off))
259 {
260 phosphor::state::manager::utils::setProperty(
261 bus, hostPath, HostState::interface, "RestartCause",
262 convertForMessage(
263 server::Host::RestartCause::PowerPolicyPreviousState));
264 phosphor::state::manager::utils::setProperty(
265 bus, hostPath, HostState::interface,
266 "RequestedHostTransition",
267 convertForMessage(server::Host::Transition::On));
268 }
269 }
270 }
271 catch (const sdbusplus::exception_t& e)
272 {
273 error("Error in PowerRestorePolicy Get: {ERROR}", "ERROR", e);
274 elog<InternalFailure>();
275 }
276
277 return 0;
278 }
279